Imports System.Windows.Forms
Imports Vintasoft.Imaging.Codecs

Namespace Imaging.Codecs
	''' <summary>
	''' Class that contains filters for Open/Save dialogs.
	''' </summary>
	Public Class CodecsFileFilters

		#Region "Constants"

		Const Filters_ImagesFiles As String = "BMP Files|*.bmp|JPEG Files|*.jpg;*.jpeg|PNG Files|*.png|TIFF Files|*.tif;*.tiff|GIF Files|*.gif"
		Const Filters_AllImageFiles As String = "All Image Files|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff;*.gif;"

		Const Filters_Extended As String = "*.wmf;*.emf;*.ico;"
		Const Filters_ExtendedFiles As String = "EMF Files|*.emf|WMF Files|*.wmf|Icon files|*.ico"

		Const Filters_TiffFiles As String = "TIFF Files|*.tif;*.tiff"
		Const Filters_JpegFiles As String = "JPEG Files|*.jpg;*.jpeg"

		Const Filters_Pdf As String = "*.pdf"
		Const Filters_PdfFiles As String = "PDF Files|" + Filters_Pdf

		Const Filters_Gif As String = "*.gif"
		Const Filters_GifFiles As String = "GIF Files|" + Filters_Gif

		Const Filters_Png As String = "*.png"
		Const Filters_PngFiles As String = "PNG Files|" + Filters_Png

		Const Filters_Jbig2 As String = "*.jb2;*.jbig2"
		Const Filters_Jbig2Files As String = "JBIG2 Files|" + Filters_Jbig2

		Const Filters_Jpeg2000 As String = "*.jp2;*.j2k;*.j2c;*.jpc"
		Const Filters_Jpeg2000Files As String = "JPEG 2000 Files|" + Filters_Jpeg2000

		#End Region



		#Region "Properties"

		#Region "Decoder"

		Shared _decoderFilters As String = Nothing
		Public Shared ReadOnly Property DecoderFilters() As String
			Get
				If _decoderFilters Is Nothing Then
					SetDecoderFilters()
				End If
				Return _decoderFilters
			End Get
		End Property

		Shared _decoderFiltersDefaultFilterIndex As Integer = -1
		Public Shared ReadOnly Property DecoderFiltersDefaultFilterIndex() As Integer
			Get
				If _decoderFiltersDefaultFilterIndex = -1 Then
					SetDecoderFilters()
				End If
				Return _decoderFiltersDefaultFilterIndex
			End Get
		End Property

		#End Region


		#Region "Encoder"

		Shared _encoderFilters As String = Nothing
		Public Shared ReadOnly Property EncoderFilters() As String
			Get
				If _encoderFilters Is Nothing Then
					SetEncoderFilters()
				End If
				Return _encoderFilters
			End Get
		End Property

		Shared _encoderFiltersDefaultFilterIndex As Integer = -1
		Public Shared ReadOnly Property EncoderFiltersDefaultFilterIndex() As Integer
			Get
				If _encoderFiltersDefaultFilterIndex = -1 Then
					SetEncoderFilters()
				End If
				Return _encoderFiltersDefaultFilterIndex
			End Get
		End Property

		Shared _multiPageEncoderFilters As String = Nothing
		Public Shared ReadOnly Property MultipageEncoderFilters() As String
			Get
				If _multiPageEncoderFilters Is Nothing Then
					SetMultiPageEncoderFilters()
				End If
				Return _multiPageEncoderFilters
			End Get
		End Property


		Shared _multiPageEncoderFiltersDefaultFilterIndex As Integer = -1
		Public Shared ReadOnly Property MultipageEncoderFiltersDefaultFilterIndex() As Integer
			Get
				If _multiPageEncoderFiltersDefaultFilterIndex = -1 Then
					SetMultiPageEncoderFilters()
				End If
				Return _multiPageEncoderFiltersDefaultFilterIndex
			End Get
		End Property


		Shared _encoderFiltersWithAnnotations As String = Nothing
		Public Shared ReadOnly Property EncoderFiltersWithAnnotations() As String
			Get
				If _encoderFiltersWithAnnotations Is Nothing Then
					SetEncoderFiltersWithAnnotations()
				End If
				Return _encoderFiltersWithAnnotations
			End Get
		End Property

		Shared _encoderFiltersWithAnnotationsDefaultFilterIndex As Integer = -1
		Public Shared ReadOnly Property EncoderFiltersWithAnnotationsDefaultFilterIndex() As Integer
			Get
				If _encoderFiltersWithAnnotationsDefaultFilterIndex = -1 Then
					SetEncoderFiltersWithAnnotations()
				End If
				Return _encoderFiltersWithAnnotationsDefaultFilterIndex
			End Get
		End Property

		Shared _multiPageEncoderFiltersWithAnnotations As String = Nothing
		Public Shared ReadOnly Property MultipageEncoderFiltersWithAnnotations() As String
			Get
				If _multiPageEncoderFiltersWithAnnotations Is Nothing Then
					SetMultiPageEncoderFiltersWithAnnotations()
				End If
				Return _multiPageEncoderFiltersWithAnnotations
			End Get
		End Property

		Shared _multiPageEncoderFiltersWithAnnotationsDefaultFilterIndex As Integer = -1
		Public Shared ReadOnly Property MultipageEncoderFiltersWithAnnotationsDefaultFilterIndex() As Integer
			Get
				If _multiPageEncoderFiltersWithAnnotationsDefaultFilterIndex = -1 Then
					SetMultiPageEncoderFiltersWithAnnotations()
				End If
				Return _multiPageEncoderFiltersWithAnnotationsDefaultFilterIndex
			End Get
		End Property

		#End Region

		#End Region



		#Region "Methods"

		#Region "Decoder"

		Private Shared Sub SetDecoderFilters()
			Dim filter1 As String = String.Format("{0}|{1}", Filters_ImagesFiles, Filters_ExtendedFiles)
			Dim filter2 As String = Filters_AllImageFiles & Filters_Extended
			_decoderFiltersDefaultFilterIndex = 9

			If AvailableDecoders.IsDecoderAvailable("Jbig2") Then
				filter1 += "|" & Filters_Jbig2Files
				filter2 += Filters_Jbig2 & ";"
				_decoderFiltersDefaultFilterIndex += 1
			End If

			If AvailableDecoders.IsDecoderAvailable("Pdf") Then
				filter1 += "|" & Filters_PdfFiles
				filter2 += Filters_Pdf & ";"
				_decoderFiltersDefaultFilterIndex += 1
			End If

			If AvailableDecoders.IsDecoderAvailable("Jpeg2000") Then
				filter1 += "|" & Filters_Jpeg2000Files
				filter2 += Filters_Jpeg2000 & ";"
				_decoderFiltersDefaultFilterIndex += 1
			End If

			_decoderFilters = String.Format("{0}|{1}", filter1, filter2)
		End Sub


		Public Shared Sub SetFilters(dialog As OpenFileDialog)
			dialog.Filter = DecoderFilters
			dialog.FilterIndex = DecoderFiltersDefaultFilterIndex
		End Sub

		Public Shared Sub SetFilters(dialog As SaveFileDialog, multipageOnly As Boolean)
			If multipageOnly Then
				dialog.Filter = MultipageEncoderFilters
				dialog.FilterIndex = MultipageEncoderFiltersDefaultFilterIndex
				dialog.DefaultExt = ".tif"
			Else
				dialog.Filter = EncoderFilters
				dialog.FilterIndex = EncoderFiltersDefaultFilterIndex
				dialog.DefaultExt = ".png"
			End If
			dialog.OverwritePrompt = False
		End Sub

		Public Shared Sub SetFiltersWithAnnotations(dialog As SaveFileDialog, multipageOnly As Boolean)
			If multipageOnly Then
				dialog.Filter = MultipageEncoderFiltersWithAnnotations
				dialog.FilterIndex = MultipageEncoderFiltersWithAnnotationsDefaultFilterIndex
			Else
				dialog.Filter = EncoderFiltersWithAnnotations
				dialog.FilterIndex = EncoderFiltersWithAnnotationsDefaultFilterIndex
			End If
			dialog.OverwritePrompt = False
			dialog.DefaultExt = ".tif"
		End Sub

		#End Region


		#Region "Encoder"

		Private Shared Sub SetEncoderFilters()
			Dim filter1 As String = Filters_ImagesFiles
			_encoderFiltersDefaultFilterIndex = 3

			If AvailableEncoders.IsEncoderAvailable("Jbig2") Then
				filter1 += "|" & Filters_Jbig2Files
			End If

			If AvailableEncoders.IsEncoderAvailable("Pdf") Then
				filter1 += "|" & Filters_PdfFiles
			End If

			If AvailableEncoders.IsEncoderAvailable("Jpeg2000") Then
				filter1 += "|" & Filters_Jpeg2000Files
			End If

			_encoderFilters = filter1
		End Sub

		Private Shared Sub SetMultiPageEncoderFilters()
			Dim filter1 As String = "TIFF Files|*.tif;*.tiff"
			_multiPageEncoderFiltersDefaultFilterIndex = 0

			If AvailableEncoders.IsEncoderAvailable("Jbig2") Then
				filter1 += "|" & Filters_Jbig2Files
			End If

			If AvailableEncoders.IsEncoderAvailable("Pdf") Then
				filter1 += "|" & Filters_PdfFiles
			End If

			filter1 += "|" & Filters_GifFiles

			_multiPageEncoderFilters = filter1
		End Sub


		Private Shared Sub SetEncoderFiltersWithAnnotations()
			Dim filter1 As String = Filters_TiffFiles
			_encoderFiltersWithAnnotationsDefaultFilterIndex = 0

			If AvailableEncoders.IsEncoderAvailable("Pdf") Then
				filter1 += "|" & Filters_PdfFiles
			End If

			filter1 += "|" & Filters_JpegFiles

			filter1 += "|" & Filters_PngFiles

			_encoderFiltersWithAnnotations = filter1
		End Sub

		Private Shared Sub SetMultiPageEncoderFiltersWithAnnotations()
			Dim filter1 As String = Filters_TiffFiles
			_multiPageEncoderFiltersWithAnnotationsDefaultFilterIndex = 0

			If AvailableEncoders.IsEncoderAvailable("Pdf") Then
				filter1 += "|" & Filters_PdfFiles
			End If

			_multiPageEncoderFiltersWithAnnotations = filter1
		End Sub


		#End Region

		#End Region

	End Class
End Namespace
